#version 460
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
//#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
#extension GL_ARB_shader_clock : enable

#include "../../../core/shader_std.h"
#include "../../../core/shader_built_in_functions.h"



layout(location = 0) in vec4 vColor;
layout(location = 1) in vec2 vUV;
layout(location = 2) in flat uint vLayerNum;

layout(location=0) out vec4 outColor;



layout(set = 3, binding = DEF_binding_L3_ArrayImage, rgba8) uniform image2D		image[];
layout(set = 3, binding = DEF_binding_L3_ArraySampler)		uniform sampler2D	tex[];

layout(std140, set = 4, scalar, column_major, binding = DEF_binding_L4_SampleAA) uniform U_0 {
	S_FrameSample_TAA m_SamplerAttr[DEF_UI_LayerNum];
};



void main(void) {
	vec4 blurColor = vec4(0);
	const vec2 texSize = vec2(imageSize(image[0]));
	const ivec2 uv = ivec2(vUV * texSize);
	const vec2 offset = 1 / texSize;

	float aBlur = 0;

	outColor = vec4(vec3(0), 0);
	//vec4 color[8];
	
	//outColor = vec4(vec3(imageLoad(image[1], uv)), 1);
	//return;
	/*for ( int i = 0; i < vLayerNum; ++i ) {
		vec4 layerColorTex = imageLoad(image[i], uv);
		outColor.rgb = mix(outColor.rgb, layerColorTex.rgb, layerColorTex.a);
		outColor.a = min(layerColorTex.a + outColor.a, 1);
	}
	return;*/

	for ( int i = 0; i < vLayerNum; ++i ) {
		uint index = i * 3;
		vec4 blur = texture(tex[index + 2], vUV);
		//aBlur = max(aBlur, blur.r);

		vec4 colorBack = imageLoad(image[i], uv);
		vec4 blurSample = texture(tex[index], vUV + offset);
		blurColor = mix(blurColor, blurSample, blurSample.a);

		if (blur.r > 0) {
			colorBack = mix(blurColor, colorBack, colorBack.a);
			colorBack.a = min(colorBack.a + blur.r, 1);
		}
		
		outColor.rgb = mix(outColor.rgb, colorBack.rgb, colorBack.a);
		//outColor.rgb = outColor.rgb + colorBack.rgb;
		outColor.a = min(colorBack.a + outColor.a, 1);
		
		//outColor = vec4(vec3(imageLoad(image[i], uv)), 1);
		//if(outColor.a >= 1) break;
	}
	
}


// textureGather()